package br.com.fafica.sisape.view;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.List;
import javax.swing.JButton;

import br.com.fafica.sisape.control.ControllerPrincipal;
import br.com.fafica.sisape.model.Exame;
import br.com.fafica.sisape.model.Medico;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class TelaListarMedicos extends JFrame {

	static Integer codClicado;
	private JPanel contentPane;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TelaListarMedicos frame = new TelaListarMedicos();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public TelaListarMedicos() {
		setResizable(false);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 659, 433);
		contentPane = new JPanel();
		contentPane.setBackground(Color.WHITE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblMedico = new JLabel("Medico:");
		lblMedico.setFont(new Font("Times New Roman", Font.PLAIN, 16));
		lblMedico.setBounds(10, 11, 75, 23);
		contentPane.add(lblMedico);
		
		final List list = new List();
		list.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				codClicado = list.getSelectedIndex();
			}
		});
		list.setBounds(10, 36, 625, 277);
		contentPane.add(list);
		
		JButton btnVizualizar = new JButton("Vizualizar");
		btnVizualizar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				ControllerPrincipal control = new ControllerPrincipal();
				ArrayList listaMedico = control.listarMedicos();
				Medico m = (Medico) listaMedico.get(codClicado);
				TelaCadastroMedico.txt_nome.setText(m.getNome());
				TelaCadastroMedico.txt_telefone.setText(m.getTelefone());
				TelaCadastroMedico.txt_crm.setText(m.getCrm());
				TelaCadastroMedico.txt_email.setText(m.getEmail());
				TelaCadastroMedico.codAletrar = m.getCod();
				dispose();
			}
		});
		btnVizualizar.setFont(new Font("Times New Roman", Font.PLAIN, 16));
		btnVizualizar.setBounds(356, 350, 105, 33);
		contentPane.add(btnVizualizar);
		
		JButton btnSair = new JButton("Sair");
		btnSair.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		btnSair.setFont(new Font("Times New Roman", Font.PLAIN, 16));
		btnSair.setBounds(471, 350, 105, 33);
		contentPane.add(btnSair);
		
		ControllerPrincipal control = new ControllerPrincipal();
		ArrayList lista = control.listarMedicos();
		String codMed;
		String nome;
		String telefone;
		String email;
		String crm;
		
		Medico med;
		for(int i = 0; i < lista.size(); i++){
			med = (Medico) lista.get(i);
			codMed = Integer.toString(med.getCod());
			nome = med.getNome();
			telefone = med.getTelefone();
			email = med.getEmail();
			crm = med.getCrm();
			list.add("Cod: "+codMed+"  |  Nome: "+nome+"  |  Telefone: "
			+telefone+"  |  Email: "+email+"  |  CRM: "+crm, med.getCod());
		}
	}

}
